package com.it.context;

import lombok.extern.slf4j.Slf4j;

/**
 * 基于ThreadLocal封装工具类，用于保存和获取当前登录用户id
 */
@Slf4j
public class BaseContext {

    public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    /**
     * 设置当前登录用户id
     * @param id
     */
    public static void setCurrentId(Long id) {
        log.info("BaseContext.setCurrentId() 被调用，设置用户ID: {}", id);
        threadLocal.set(id);
        log.info("BaseContext.setCurrentId() 完成，当前ThreadLocal中的值: {}", threadLocal.get());
    }

    /**
     * 获取当前登录用户id
     * @return
     */
    public static Long getCurrentId() {
        Long userId = threadLocal.get();
        log.info("BaseContext.getCurrentId() 被调用，返回用户ID: {}", userId);
        return userId;
    }

    /**
     * 移除当前登录用户id
     */
    public static void removeCurrentId() {
        log.info("BaseContext.removeCurrentId() 被调用");
        threadLocal.remove();
    }

}
